<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页面</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#165DFF',
                        secondary: '#4080FF',
                        neutral: '#F2F3F5',
                        'neutral-dark': '#86909C',
                        'neutral-deeper': '#1D2129',
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                    boxShadow: {
                        'card': '0 10px 30px -5px rgba(0, 0, 0, 0.1)',
                    }
                },
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .bg-gradient-primary {
                background: linear-gradient(135deg, #165DFF 0%, #4080FF 100%);
            }
            .input-focus {
                @apply focus:border-primary focus:ring-2 focus:ring-primary/20 focus:outline-none;
            }
            .btn-hover {
                @apply hover:shadow-lg hover:-translate-y-0.5 transition-all duration-300;
            }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen font-inter">
<div class="flex items-center justify-center min-h-screen px-4 sm:px-6 lg:px-8">
    <div class="w-full max-w-md space-y-8 bg-white rounded-2xl shadow-card p-8">
        <div class="text-center">
            <div class="flex items-center justify-center mb-6">
                <div class="bg-gradient-primary text-white p-3 rounded-xl">
                    <i class="fa fa-lock text-2xl"></i>
                </div>
            </div>
            <h2 class="text-[clamp(1.5rem,3vw,2rem)] font-bold text-neutral-deeper">账户登录</h2>
            <p class="mt-2 text-neutral-dark">请输入您的账号和密码</p>
        </div>

        <form id="loginForm" class="mt-8 space-y-6">
            <div class="rounded-xl shadow-sm -space-y-px">
                <div class="mb-4">
                    <label for="username" class="block text-sm font-medium text-neutral-deeper mb-1">账号</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-user text-neutral-dark"></i>
                        </div>
                        <input id="username" name="username" type="text" required
                               class="w-full pl-10 pr-3 py-3 border border-gray-300 rounded-xl input-focus transition duration-200 ease-in-out"
                               placeholder="请输入账号">
                    </div>
                </div>

                <div class="mb-6">
                    <label for="password" class="block text-sm font-medium text-neutral-deeper mb-1">密码</label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fa fa-lock text-neutral-dark"></i>
                        </div>
                        <input id="password" name="password" type="password" required
                               class="w-full pl-10 pr-10 py-3 border border-gray-300 rounded-xl input-focus transition duration-200 ease-in-out"
                               placeholder="请输入密码">
                        <button type="button" id="togglePassword" class="absolute inset-y-0 right-0 pr-3 flex items-center text-neutral-dark hover:text-primary transition-colors">
                            <i class="fa fa-eye-slash"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="flex items-center justify-between">
                <div class="flex items-center">
                    <input id="remember-me" name="remember-me" type="checkbox"
                           class="h-4 w-4 text-primary focus:ring-primary border-gray-300 rounded">
                    <label for="remember-me" class="ml-2 block text-sm text-neutral-dark">记住密码</label>
                </div>

                <div class="text-sm">
                    <a href="#" class="font-medium text-primary hover:text-secondary transition-colors">忘记密码?</a>
                </div>
            </div>

            <div>
                <button type="submit"
                        class="w-full flex justify-center py-3 px-4 border border-transparent rounded-xl bg-gradient-primary text-base font-medium text-white btn-hover">
                    <i class="fa fa-sign-in mr-2"></i>登录
                </button>
            </div>

            <div class="text-center text-sm text-neutral-dark">
                还没有账号? <a href="#" class="font-medium text-primary hover:text-secondary transition-colors">立即注册</a>
            </div>
        </form>
    </div>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        const loginForm = document.getElementById('loginForm');
        const togglePassword = document.getElementById('togglePassword');
        const passwordInput = document.getElementById('password');

        // 切换密码可见性
        togglePassword.addEventListener('click', function() {
            const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
            passwordInput.setAttribute('type', type);

            // 切换图标
            const icon = togglePassword.querySelector('i');
            if (type === 'text') {
                icon.classList.remove('fa-eye-slash');
                icon.classList.add('fa-eye');
            } else {
                icon.classList.remove('fa-eye');
                icon.classList.add('fa-eye-slash');
            }
        });

        // 登录表单提交处理
        loginForm.addEventListener('submit', function(e) {
            e.preventDefault();

            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;

            // 显示加载状态
            const submitButton = loginForm.querySelector('button[type="submit"]');
            const originalButtonText = submitButton.innerHTML;
            submitButton.disabled = true;
            submitButton.innerHTML = '<i class="fa fa-spinner fa-spin mr-2"></i>登录中...';

            // 发送登录请求
            fetch(`/login?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络错误，请稍后再试');
                    }
                    return response.json();
                })
                .then(data => {
                    if (data.success) {
                        // 登录成功，跳转到部门页面
                        localStorage.setItem('username', data.data);
                        window.location.href = 'department.html';
                    } else {
                        // 登录失败，显示错误提示
                        showErrorToast('账号或密码错误');
                    }
                })
                .catch(error => {
                    console.error('登录错误:', error);
                    showErrorToast('登录失败，请检查网络连接');
                })
                .finally(() => {
                    // 恢复按钮状态
                    submitButton.disabled = false;
                    submitButton.innerHTML = originalButtonText;
                });
        });

        // 显示错误提示
        function showErrorToast(message) {
            // 检查是否已存在toast元素
            let toast = document.getElementById('errorToast');
            if (toast) {
                toast.remove();
            }

            // 创建toast元素
            toast = document.createElement('div');
            toast.id = 'errorToast';
            toast.className = 'fixed bottom-4 left-1/2 transform -translate-x-1/2 bg-red-500 text-white px-6 py-3 rounded-lg shadow-lg z-50 flex items-center opacity-0 transition-opacity duration-300';
            toast.innerHTML = `
                    <i class="fa fa-exclamation-circle mr-2"></i>
                    <span>${message}</span>
                `;

            document.body.appendChild(toast);

            // 显示toast
            setTimeout(() => {
                toast.style.opacity = '1';
            }, 10);

            // 3秒后隐藏toast
            setTimeout(() => {
                toast.style.opacity = '0';
                setTimeout(() => {
                    toast.remove();
                }, 300);
            }, 3000);
        }
    });
</script>
</body>
</html>
